package org.dfl.plugin.editor.actions;

import java.util.List;

import org.dfl.messages.Messages;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.editor.commands.CancelCommandRuntimeException;
import org.dfl.plugin.editor.commands.TransitionEditProcesorCommand;
import org.dfl.plugin.editor.model.TransitionModelElement;
import org.eclipse.gef.EditPart;
import org.eclipse.ui.IWorkbenchPart;

/**
 * edit taverna processor properties action
 * @author Piotr Wlodarczyk
 *
 */
public class TavernaTransitionEditAction extends
		org.eclipse.gef.ui.actions.SelectionAction {

	public static final String EDIT_TAVERNA = "EditTaverna"; //$NON-NLS-1$

	public TavernaTransitionEditAction(IWorkbenchPart part) {
		super(part);
		setText(Messages.TavernaTransitionEditAction_name);
		setId(EDIT_TAVERNA);
		setToolTipText(Messages.TavernaTransitionEditAction_tooltip);
		setImageDescriptor(DflPlugin.getImageDescriptor("icons/eclipse/properties.gif")); //$NON-NLS-1$
		setHoverImageDescriptor(getImageDescriptor());
	}

	@SuppressWarnings("unchecked")
	protected boolean calculateEnabled() {
		if (getSelectedObjects().isEmpty())
			return false;
		List parts = getSelectedObjects();
		for (int i = 0; i < parts.size(); i++) {
			Object o = parts.get(i);
			if (!(o instanceof EditPart))return false;
			EditPart part = (EditPart) o;
			
			if (!(part.getModel() instanceof TransitionModelElement)) return false;
			
			TransitionModelElement tme = (TransitionModelElement) part.getModel();
			return new TransitionEditProcesorCommand(tme).canExecute();
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	public void run() {
		List editparts = getSelectedObjects();
		for (int j = 0; j < editparts.size(); j++) {
			EditPart part = (EditPart) editparts.get(j);
			TransitionModelElement tme = (TransitionModelElement) part.getModel();
			try{
				execute(new TransitionEditProcesorCommand(tme));
			}catch(CancelCommandRuntimeException e){}
		}
	}

}
